package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.BizConsumer;
import com.situation.pojo.DO.BizInStock;
import com.situation.pojo.DO.BizProduct;
import com.situation.pojo.DO.BizSupplier;
import com.situation.pojo.VO.InStockInfoProVO;
import com.situation.pojo.VO.InStockVO;
import com.situation.service.impl.BizSupplierServiceImp;
import com.situation.service.impl.InStockServiceImpl;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 羽程
 * @create: 2021-12-06 21:43
 **/
@RestController
@RequestMapping("/api/inStock")
public class InStockController {
    @Autowired
    private InStockServiceImpl inStockService;
    @Autowired
    private BizSupplierServiceImp bizSupplierService;

    /**
     * 物资入库
     */
    @ControllerEndpoint(exceptionMessage = "添加入库单失败",operation = "添加入库单")
    @RequestMapping("/addIntoStock")
    public RespUtil addIntoStock(InStockVO inStockVO)throws IllegalAccessException{
        if (inStockVO.getId() == null) {
            //说明现在添加物资来源
            if ("".equals(inStockVO.getName()) || inStockVO.getName() == null) {
                throw new IllegalAccessException("物资去向名不能为空");
            }
            if ("".equals(inStockVO.getContact()) || inStockVO.getContact() == null) {
                throw new IllegalAccessException("联系人不能为空");
            }
            if ("".equals(inStockVO.getAddress()) || inStockVO.getAddress() == null) {
                throw new IllegalAccessException("地址不能为空");
            }
            if ("".equals(inStockVO.getPhone()) || inStockVO.getPhone() == null) {
                throw new IllegalAccessException("联系方式不能为空");
            }
            if (inStockVO.getSort() == null) {
                throw new IllegalAccessException("排序不能为空");
            }
            BizSupplier bizSupplier = new BizSupplier();
            bizSupplier.setAddress(inStockVO.getAddress());
            bizSupplier.setContact(inStockVO.getContact());
            bizSupplier.setCreateTime(LocalDateTime.now());
            bizSupplier.setModifiedTime(LocalDateTime.now());
            bizSupplier.setName(inStockVO.getName());
            bizSupplier.setPhone(inStockVO.getPhone());
            bizSupplier.setSort(inStockVO.getSort());
            BizSupplier supplier = bizSupplierService.insert(bizSupplier);
            inStockVO.setId(supplier.getId());
        }
            return RespUtil.ok().setData(1);
    }

    /**
     * 在回收站中恢复入库单
     * @param bizInStock
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "恢复入库单失败",operation = "恢复入库单")
    @RequestMapping("/back")
    @RequiresPermissions({"inStock:back"})
    public RespUtil back(BizInStock bizInStock){

        return RespUtil.ok().setData(inStockService.back(bizInStock));
    }

    /**
     * 移入回收站
     */
    @ControllerEndpoint(exceptionMessage = "入库单回收失败",operation = "入库单回收")
    @RequestMapping("/remove/{id}")
    @RequiresPermissions({"inStock:remove"})
    public RespUtil remove(@PathVariable Long id){
        BizInStock bizInStock = new BizInStock();
        bizInStock.setId(id);
        return RespUtil.ok().setData(inStockService.remove(bizInStock));
    }

    /**
     * 查询明细表分页
     * @param bizInStock
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions({"inStock:detail"})
    public RespUtil detail(BizInStock bizInStock,int pageNum,int pageSize){
        Map map=new HashMap();
        BizSupplier bizSupplier = inStockService.detail1(bizInStock);
        PageInfo<InStockInfoProVO> pageInfo = inStockService.detail2(bizInStock, pageNum, pageSize);
        map.put("明细头",bizSupplier);
        map.put("明细表",pageInfo);
        return RespUtil.ok().setData(map);
    }

    @ControllerEndpoint(exceptionMessage = "删除入库单失败",operation = "删除入库单")
    @RequestMapping("/delete")
    @RequiresPermissions({"inStock:delete"})
    public RespUtil delete(String inNum){
        inStockService.delete(inNum);
        return RespUtil.ok();
    }

    @RequestMapping("/findInStockList")
    public RespUtil findInStockList(InStockVO inStockVO,int pageNum,int pageSize){
        PageInfo<InStockVO> inStockList = inStockService.findInStockList(inStockVO, pageNum, pageSize);
        return RespUtil.ok().setData(inStockList);
    }

    @ControllerEndpoint(exceptionMessage = "入库单审核失败",operation = "入库单审核")
    @RequestMapping("/publish")
    @RequiresPermissions({"inStock:publish"})
    public RespUtil publish(BizInStock bizInStock){
        return RespUtil.ok().setData(inStockService.publish(bizInStock));
    }
}
